package com.chengyu.core.controller.goods;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.UserBaseController;
import com.chengyu.core.domain.enums.GoodsEnums;
import com.chengyu.core.domain.enums.OrderEnums;
import com.chengyu.core.domain.form.GoodsPublishForm;
import com.chengyu.core.domain.form.GoodsSearchForm;
import com.chengyu.core.domain.form.OrderCommentSearchForm;
import com.chengyu.core.domain.form.OrderGroupSearchForm;
import com.chengyu.core.domain.result.GoodsResult;
import com.chengyu.core.domain.result.OrderCommentResult;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.*;
import com.chengyu.core.request.PageParamRequest;
import com.chengyu.core.service.goods.GoodsParamsService;
import com.chengyu.core.service.goods.GoodsService;
import com.chengyu.core.service.member.MemberCollectGoodsService;
import com.chengyu.core.service.member.MemberCollectShopService;
import com.chengyu.core.service.member.MemberViewGoodsService;
import com.chengyu.core.service.order.OrderCommentService;
import com.chengyu.core.service.order.OrderGroupService;
import com.chengyu.core.service.shop.ShopCouponService;
import com.chengyu.core.service.shop.ShopFreightService;
import com.chengyu.core.service.shop.ShopService;
import com.chengyu.core.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "宠物")
@Controller
@RequestMapping("/pets")
public class PetsController extends UserBaseController {

	@Autowired
	private GoodsService goodsService;
	@Autowired
	private ShopService shopService;
	@Autowired
	private MemberViewGoodsService memberViewGoodsService;
	@Autowired
	private GoodsParamsService goodsParamsService;
	@Autowired
	private MemberCollectGoodsService memberCollectGoodsService;
	@Autowired
	private MemberCollectShopService memberCollectShopService;
	@Autowired
	private ShopFreightService shopFreightService;
	@Autowired
	private ShopCouponService shopCouponService;
	@Autowired
	private OrderCommentService orderCommentService;
	@Autowired
	private OrderGroupService orderGroupService;

	@ApiOperation(value = "搜索商品(查询商品列表大部分用此接口)")
	@ResponseBody
	@RequestMapping(value="/goods/search", method=RequestMethod.GET)
	public CommonResult<CommonPage<PmsGoods>> search(GoodsSearchForm form,  @Validated PageParamRequest pageParamRequest) {
		form.setStatus(GoodsEnums.GoodsStatus.SELL.getValue());
		List<PmsGoods> list = goodsService.getGoodsList(form, pageParamRequest.getPage(),pageParamRequest.getPageSize());
		return CommonResult.success(CommonPage.restPage(list));
	}

//	@ApiOperation(value = "商品列表")
//	@ResponseBody
//	@RequestMapping(value="/goods/getList", method=RequestMethod.GET)
//	public CommonResult<CommonPage<PmsGoods>> getList(GoodsSearchForm form,
//													  @RequestParam(value = "page", defaultValue = "1") Integer page,
//													  @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
//
////		String json = "[{\"name\":\"Alice\",\"age\":20,\"address\":\"123 Main St\"},{\"name\":\"Bob\",\"age\":25,\"address\":\"456 Elm St\"}]";
////		Gson gson = new Gson();
////		List<User> userList = gson.fromJson(json, new TypeToken<List<User>>() {}.getType());
////		for (User user : userList) {
////			System.out.println(user.getName());
////			System.out.println(user.getAge());
////			System.out.println(user.getAddress());
////		}
////		System.out.println("type"+skuList);
//
//		Integer type = form.getType();
//
//		System.out.println("type"+type);
//		if (type == null || type.equals(0)) {
//			form.setType(GoodsEnums.GoodsType.NORMAL_GOODS.getValue());
//		}
//		form.setShopId(getCurrentShop().getId());
//		List<PmsGoods> list = goodsService.getGoodsList(form, page, pageSize);
//		return CommonResult.success(CommonPage.restPage(list));
//	}

//	@ApiOperation(value = "商品库商品列表")
//	@ResponseBody
//	@RequestMapping(value="/goods/getPlatformList", method=RequestMethod.GET)
//	public CommonResult<CommonPage<PmsGoods>> getPlatformList(GoodsSearchForm form,
//															  @RequestParam(value = "page", defaultValue = "1") Integer page,
//															  @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) throws ServiceException {
//		form.setQueryPlatformGoods(true);
//		List<PmsGoods> list = goodsService.getGoodsList(form, page, pageSize);
//		return CommonResult.success(CommonPage.restPage(list));
//	}
//
//	@ApiOperation(value = "商品详情")
//	@ResponseBody
//	@RequestMapping(value="/goods/get", method=RequestMethod.GET)
//	public CommonResult<GoodsResult> get(Integer goodsId) {
//		GoodsResult goods = goodsService.getGoodsResult(goodsId);
//		return CommonResult.success(goods);
//	}

	@OperationLog
	@ApiOperation(value = "添加编辑商品")
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public CommonResult<String> editSubmit(GoodsPublishForm publishForm, String groupJson) throws ServiceException {
		if(StringUtils.isNotBlank(groupJson)){
			publishForm.setGroupList(JSONArray.parseArray(groupJson, PmsGoodsGroup.class));
		}
		publishForm.setType(11);
		publishForm.setCateId(199);
		publishForm.setShopCateId(0);
		publishForm.setStatus(GoodsEnums.GoodsStatus.SELL.getValue());
		goodsService.publishGoods(getCurrentShop(), publishForm);
//		goodsService.publishGoodsNew(getCurrentShop(), publishForm);
		return CommonResult.success(null);
	}


}
